/**
 * author      : dormi330
 * date        : 2018/6/24
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.application.order;

import com.deep.trade.adaptor.required.IdService;
import com.deep.trade.adaptor.required.sku.ItemSkuService;
import com.deep.trade.adaptor.required.sku.Sku;
import com.deep.trade.application.OrderServiceFacade;
import com.deep.trade.order.*;
import com.deep.trade.order.query.OutDtoOrderDetail;
import com.deep.trade.order.query.OutDtoOrderSku;
import com.deep.trade.order.query.OutDtoOrderSummary;
import com.deep.trade.order.query.OutDtoTradeOrder;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

public class OrderBaseOperation {

    protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    protected final int pageNum = 1;
    protected final int pageSize = 10;
    protected boolean enableDebug = false;
    protected final int EXPECT_TRADE_ORDER_NUM = 2;
    protected final int EXPECT_SKU_NUM_PER_ORDER = 2;

    protected final String tenantCode = IdService.TENANT_CODE;
    //    protected final String memberId = IdService.MEMBER_ID;
    protected final String token = IdService.TOKEN;
    protected final String addressId = IdService.ADDRESS_ID;

    protected final Sku sku1 = ItemSkuService.sku1;
    protected final Sku sku2 = ItemSkuService.sku2;

    @Autowired
    protected OrderServiceFacade orderServiceFacade;

    protected void placeOrder() throws Exception {
        /*
         *
         * 同一个用户 在店铺1买了2各sku, 在店铺2买了2各sku, 下单
         * 会产生1个 OrderSummary, 2个 TradeOrder, 每个TradeOrder 会有2个OrderSku
         */
        if (enableDebug) logger.info("【start】placeOrder");

        InDtoPlaceOrder inDtoPlaceOrder = new InDtoPlaceOrder(tenantCode, token, addressId, "remark1");
        inDtoPlaceOrder
                .addSkuAndQty(IdService.SKU_1_ID, 10)
                .addSkuAndQty(IdService.SKU_2_ID, 20)
                .addSkuAndQty(IdService.SKU_3_ID, 30)
                .addSkuAndQty(IdService.SKU_4_ID, 40);
        logger.info("下单={}", inDtoPlaceOrder.toString());

        orderServiceFacade.placeOrder(inDtoPlaceOrder);
        if (enableDebug) logger.info("【√】placeOrder - done");
    }

    protected List<OutDtoTradeOrder> tradeOrderListOfMember() throws Exception {
        // ==== check TradeOrder list
        if (enableDebug) logger.info("【start】tradeOrderListOfMember");
        PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfMember(tenantCode, token, pageNum, pageSize);
        List<OutDtoTradeOrder> outDtoTradeOrderList = pageInfo.getList();
        if (enableDebug) logger.info(outDtoTradeOrderList.toString());
        if (enableDebug) logger.info("【√】tradeOrderListOfMember - done");
        return outDtoTradeOrderList;
    }

    protected OutDtoOrderDetail tradeOrderDetail(OutDtoTradeOrder outDtoTradeOrder) throws Exception {

        // ==== check order detail
        if (enableDebug) logger.info("【start】tradeOrderDetail");
        OutDtoOrderDetail outDtoOrderDetail = orderServiceFacade.tradeOrderDetail(tenantCode, outDtoTradeOrder.getId());
        if (enableDebug) logger.info(outDtoOrderDetail.toString());
        if (enableDebug) logger.info("【√】tradeOrderDetail - done");
        return outDtoOrderDetail;
    }

    protected void auditTradeOrderPass(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getSellerId(),
                outDtoTradeOrder.getId(),
                true,
                null
        );
        orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
    }

    protected void cancelTradeOrder(InDtoCancelOrder inDtoCancelOrder) throws Exception {
        orderServiceFacade.cancelTradeOrder(inDtoCancelOrder);
    }

    protected void auditTradeOrderAllPass(List<OutDtoTradeOrder> outDtoTradeOrderList) throws Exception {
        for (OutDtoTradeOrder outDtoTradeOrder : outDtoTradeOrderList) {
            InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                    outDtoTradeOrder.getTenantCode(),
                    outDtoTradeOrder.getSellerId(),
                    outDtoTradeOrder.getId(),
                    true,
                    null
            );
            orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
        }
    }

    protected void auditTradeOrderReject(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getSellerId(),
                outDtoTradeOrder.getId(),
                false,
                "reject_for_test"
        );

        orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
    }

    protected List<OutDtoOrderSummary> getOrderSummary() throws Exception {
        PageInfo<OutDtoOrderSummary> page = orderServiceFacade.orderSummaryOfMember(tenantCode, token, pageNum, pageSize);
        return page.getList();
    }

    protected void payOrderSummary(OutDtoOrderSummary outDtoOrderSummary) throws Exception {
        InDtoPayOrderSummary inDtoPayOrderSummary = new InDtoPayOrderSummary(
                "pay_type_001",
                "pay_channel_001",
                outDtoOrderSummary.getTenantCode(),
                outDtoOrderSummary.getId()
        );
        orderServiceFacade.payOrderSummary(inDtoPayOrderSummary);
    }

    protected void payTradeOrder(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoPayTradeOrder inDtoPayTradeOrder = new InDtoPayTradeOrder(
                "pay_type_001",
                "pay_channel_001",
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getId()
        );
        orderServiceFacade.payTradeOrder(inDtoPayTradeOrder);
    }

    protected void addParcel(OutDtoOrderDetail outDtoOrderDetail) throws Exception {
        String tradeOrderId = outDtoOrderDetail.getOutDtoTradeOrder().getId();
        List<OutDtoOrderSku> outDtoOrderSkuList = outDtoOrderDetail.getOutDtoOrderSkuList();
        OutDtoOrderSku skuA = outDtoOrderSkuList.get(0);
        OutDtoOrderSku skuB = outDtoOrderSkuList.get(1);


        // 包裹1
        InDtoEachParcel inDtoEachParcelA = new InDtoEachParcel(tenantCode, token, tradeOrderId, "trackingNo1", "http://www.trackingNo1", "顺丰", "顺丰001");
        inDtoEachParcelA
                .addParcelItem(skuA.getId(), 1)
                .addParcelItem(skuB.getId(), 1);

        // 包裹2
        InDtoEachParcel inDtoEachParcelB = new InDtoEachParcel(tenantCode, token, tradeOrderId, "trackingNo2", "http://www.trackingNo2", "韵达", "韵达001");
        inDtoEachParcelB
                .addParcelItem(skuA.getId(), skuA.getQuantity() - 1)
                .addParcelItem(skuB.getId(), skuB.getQuantity() - 1);

        List<InDtoEachParcel> parcels = Arrays.asList(new InDtoEachParcel[]{inDtoEachParcelA, inDtoEachParcelB});
        orderServiceFacade.addParcel(parcels);
    }

}
